/*
 * $Id: SyncToHeadRevisionAction.java 23 2002-09-29 13:59:30Z bpruessmann $
 * 
 * Copyright (c) 2001,2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.ui.action;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.p4eclipse.core.PerforceRepositoryProvider;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ui.actions.TeamAction;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

/**
  * This one might obsolete SyncToHeadAction.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class SyncToHeadRevisionAction extends TeamAction
{
	/** @see TeamAction#isEnabled() */
	protected boolean isEnabled() 
		throws TeamException
	{
		IResource[] resources = getSelectedResources();
		if (resources.length == 0) 
			return false;
			
		for (int i = 0; i < resources.length; i++) 
		{
			RepositoryProvider tempProvider = RepositoryProvider.getProvider(resources[i].getProject());
			if (tempProvider == null || !tempProvider.getID().equals(PerforceUIPlugin.getTypeId()))
				return false;
				
			PerforceRepositoryProvider provider = (PerforceRepositoryProvider)tempProvider;
			if (provider.isCheckedOut(resources[i]))
				return false;
				
			if (!provider.hasRemote(resources[i])) 
				return false;				
		}
		return true;
	}

	/** @see IActionDelegate#run(IAction) */
	public void run(IAction action)
	{
		run(
			new WorkspaceModifyOperation()
			{
				public void execute(IProgressMonitor monitor) 
					throws InterruptedException, InvocationTargetException 
				{
					IResource[] resources = getSelectedResources();
					
					try
					{
						for (int i = 0, length = resources.length; i < length; i++)
						{
							IResource resource = resources[i];
							PerforceRepositoryProvider provider = (PerforceRepositoryProvider)RepositoryProvider.getProvider(resource.getProject());												
							
							switch (resource.getType())
							{
								case IResource.FILE:
									provider.sync(new IResource[]{ resource }, IResource.DEPTH_ZERO, monitor);
									break;
									
								case IResource.FOLDER:
								case IResource.PROJECT:
									provider.sync(new IResource[]{ resource }, IResource.DEPTH_INFINITE, monitor);
									break;
							
								default:
									throw new IllegalArgumentException("resource must be either PROJECT, FOLDER or FILE."); //$NON-NLS-1$
							}
						}
					}
					catch (TeamException e)
					{
						throw new InvocationTargetException(e);						
					}
				}
			},  "SyncToHeadRevisionAction failed.", PROGRESS_DIALOG);		
	}
}
